package cn.iocoder.yudao.module.pypm.controller.admin.productinfo;

import org.springframework.web.bind.annotation.*;
import jakarta.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import jakarta.validation.*;
import jakarta.validation.*;
import jakarta.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

import cn.iocoder.yudao.framework.apilog.core.annotation.ApiAccessLog;
import static cn.iocoder.yudao.framework.apilog.core.enums.OperateTypeEnum.*;

import cn.iocoder.yudao.module.pypm.controller.admin.productinfo.vo.*;
import cn.iocoder.yudao.module.pypm.dal.dataobject.productinfo.ProductInfoDO;
import cn.iocoder.yudao.module.pypm.service.productinfo.ProductInfoService;

@Tag(name = "管理后台 - 产品信息")
@RestController
@RequestMapping("/pypm/product-info")
@Validated
public class ProductInfoController {

    @Resource
    private ProductInfoService productInfoService;

    @PostMapping("/create")
    @Operation(summary = "创建产品信息")
    @PreAuthorize("@ss.hasPermission('pypm:product-info:create')")
    public CommonResult<Long> createProductInfo(@Valid @RequestBody ProductInfoSaveReqVO createReqVO) {
        return success(productInfoService.createProductInfo(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新产品信息")
    @PreAuthorize("@ss.hasPermission('pypm:product-info:update')")
    public CommonResult<Boolean> updateProductInfo(@Valid @RequestBody ProductInfoSaveReqVO updateReqVO) {
        productInfoService.updateProductInfo(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除产品信息")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('pypm:product-info:delete')")
    public CommonResult<Boolean> deleteProductInfo(@RequestParam("id") Long id) {
        productInfoService.deleteProductInfo(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得产品信息")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('pypm:product-info:query')")
    public CommonResult<ProductInfoRespVO> getProductInfo(@RequestParam("id") Long id) {
        ProductInfoDO productInfo = productInfoService.getProductInfo(id);
        return success(BeanUtils.toBean(productInfo, ProductInfoRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得产品信息分页")
    @PreAuthorize("@ss.hasPermission('pypm:product-info:query')")
    public CommonResult<PageResult<ProductInfoRespVO>> getProductInfoPage(@Valid ProductInfoPageReqVO pageReqVO) {
        PageResult<ProductInfoDO> pageResult = productInfoService.getProductInfoPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, ProductInfoRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出产品信息 Excel")
    @PreAuthorize("@ss.hasPermission('pypm:product-info:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportProductInfoExcel(@Valid ProductInfoPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<ProductInfoDO> list = productInfoService.getProductInfoPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "产品信息.xls", "数据", ProductInfoRespVO.class,
                        BeanUtils.toBean(list, ProductInfoRespVO.class));
    }

}